﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;
using PublicTemplateEditor.Helper;
using PublicTemplateEditor.Mold;
using System.Text.RegularExpressions;
using System.Text;

namespace PublicTemplateEditor.DataAccess
{
    public class TempFileDA
    {

        #region  导入导出模板
        /// <summary>
        /// 将数据写入导出库
        /// </summary>
        /// <param name="fileList"></param>
        public static void OutFile(List<TempFileInfo> fileList, string dataPath)
        {
            string sql = "insert into  template (name,type,data) values('{0}',{1},{2});\r\n";
            Dictionary<string, byte[]> pars = new Dictionary<string, byte[]>();
            string fName = string.Empty;
            StringBuilder sb = new StringBuilder();
            //string tt = string.Empty;
            foreach (TempFileInfo file in fileList)
            {
                //fName = "@data" + file.FileName.Encode().Replace(".", "").Replace("%", "").Replace("_", "").Replace("-", "").Replace("+","");
                // [.|%|_|-|+]
                Regex reg = new Regex(@"[-|%|_|.|+]", RegexOptions.RightToLeft);
                fName = "@data" + reg.Replace(file.FileName.Encode(), "");
               

                if (file.FileName.Length > 27)
                    fName = fName.Substring(0, 27);
                sb.AppendFormat(sql, file.FileName, file.FileType, fName);
                if (!pars.ContainsKey(fName))
                    pars.Add(fName, file.Data);
            }

            using (SqlLiteHelper sqlite = new SqlLiteHelper(dataPath))
            {
                sqlite.ExeSql(sb.ToString(), pars);
            }

        }

        /// <summary>
        /// 清空导出库
        /// </summary>
        public static void TruncateDataBase()
        {
            using (SqlLiteHelper sqlite = new SqlLiteHelper(@"out.bhyf"))
            {
                sqlite.ExeSql("delete from template;");
                sqlite.Compression();
            }
        }
        #endregion

        #region 导入导出素材

        /// <summary>
        /// 导出素材
        /// </summary>
        /// <param name="typeStr">类型，以逗号分隔</param>
        /// <param name="path">导出的路径</param>
        public static void OutMaterail(string typeStr, string path)
        {
            #region 取出
            string OutSql = "select distinct name,type,oldimg,tableconfig,src from m_file where type in ({0}) ;";
            OutSql = string.Format(OutSql, typeStr);
            List<object[]> objList = CommonAccess.sqlite.GetArray(OutSql);
            if (objList == null) return;
            #endregion

            #region 导出
            string InSql = "insert into template (name,type,data,other)values('{0}',{1},{2},'{3}');";
            StringBuilder sb = new StringBuilder();
            Dictionary<string, byte[]> pars = new Dictionary<string, byte[]>();
            int parsNum = 1;
            foreach (object[] obj in objList)
            {
                if (obj[1].ToString() != "9" && obj[1].ToString() != "24")
                    sb.AppendFormat(InSql, obj[0], obj[1], "@Data" + parsNum.ToString(), obj[3]);
                else
                    sb.AppendFormat(InSql, obj[0], obj[1], "@Data" + parsNum.ToString(), obj[4].ToString() + "|" + obj[3].ToString().Encode());
                pars.Add("@Data" + parsNum.ToString(), obj[2].ToByte());
                parsNum++;
            }
            
            string sql = sb.ToString();
            if (sql.IsNull())
                return;

            sql = sql.Substring(0, sb.Length - 1);

            using (SqlLiteHelper sqlite = new SqlLiteHelper(path))
            {
                sqlite.ExeSql(sql, pars);
            }
            #endregion
        }

        /// <summary>
        /// 导入素材
        /// </summary>
        /// <param name="typeStr">类型，以逗号分隔</param>
        /// <param name="path">导入的路径</param>
        public static List<TempFileInfo> InMaterail(string typeStr, string path)
        {
            string OutSql = "select name,type,data,other from template where type in ({0});";
            OutSql = string.Format(OutSql, typeStr);
            List<object[]> objList = null;
            List<TempFileInfo> fileList = new List<TempFileInfo>(); 
            using (SqlLiteHelper sqlite = new SqlLiteHelper(path))
            {
                objList = sqlite.GetArray(OutSql);
            }

            if (objList == null) return null;
                 
            
            foreach (object[] obj in objList)
            {
                fileList.Add(new TempFileInfo()
                {
                    FileName = obj[0].ToString(),
                    FileType = obj[1].ToInt(),
                    Data = obj[2].ToByte(),
                    Other = obj[3].ToString(),
                });
            }

            return fileList;
             
        }


        #endregion
    }
}
